const {randomnum,projectkeywords} = require("../utils")
const connect = require("../sql")
const jsonwebtoken = require("jsonwebtoken")
/**
 * @Description:  # TODO 获取验证码
 * @Author: 文渊
 * @Date: 2023-08-28 14:31:45
 */
function checkcode(req,res){
    //返回验证码
    res.send({code:200,msg:randomnum(1000,9999)})
}

/**
 * @Description:  # TODO 登录操作
 * @Author: 文渊
 * @Date: 2023-08-28 14:31:53
 */
async function login(req,res){
    const {username,password} = req.body;
    const sql = "select * from admin where username = ? and password = ?"
    const arr = [username,password]
    const result = await connect(sql,arr)
    //如果是查询 => 那么用长度判断
    //如果是增删改 => 那么 affectedRows
    if(result.length){
        //生成一个token
        const token = jsonwebtoken.sign({username},projectkeywords,{expiresIn:60*60})
        //登录成功,返回给前端数据
        res.send({code:200,msg:result,token})
    }else{
        res.send({code:301,msg:"登录失败"})
    }
}

//获取所有的管理员信息
async function queryall(req,res){
    //获取传入过来的权限 level 
    const {level} = req.query
    //如果权限是1 => 管理员 => 没有资格
    //如果权限是0 => 超级管理员 => 资源最大
    if(level == 0){
        const sql = "select * from admin"
        const result = await connect(sql,[])
        if(result.length){
            res.send({code:200,msg:result})
        }else{
            res.send({code:308,msg:"获取所有管理员信息失败"})
        }
    }else{
        res.send({code:200,msg:[]})
    }
}

/**
 * @Description:  # TODO 添加管理员信息
 * @Author: 文渊
 * @Date: 2023-09-04 11:05:58
 */
async function add(req,res){
    const {username,password,nickname,url,level} = req.body;
    const sql = "insert into admin values (null,?,?,?,?,?)"
    const result = await connect(sql,[username,password,nickname,url,level])
    if(result.affectedRows){
        res.send({code:200,msg:'添加成功'})
    }else{
        res.send({code:401,msg:"添加失败"})
    }
}

/* 删除管理员信息 */
async function remove(req,res){
    const {id} = req.body;
    const sql = "delete from admin where aid = ?"
    const result = await connect(sql,[id])
    if(result.affectedRows){
        res.send({code:200,msg:"删除成功"})
    }else{
        res.send({code:402,msg:"删除失败!"})
    }
}

/* 根据ID获取管理员信息 */
async function queryone(req,res){
    const {id} = req.query;
    const sql = "select * from admin where aid = ?"
    const result = await connect(sql,[id])
    if(result.length){
        res.send({code:200,msg:result})
    }else{
        res.send({code:403,msg:"根据ID获取管理员信息失败"})
    }
}

/**
 * @Description:  # TODO 修改管理员信息
 * @Author: 文渊
 * @Date: 2023-09-04 11:48:07
 */
async function edit(req,res){
    const {id,username,password,nickname,url,level} = req.body;
    const sql = "update admin set username = ? , password = ? , nickname = ? , url = ? , level = ? where aid = ?"
    const result = await connect(sql,[username,password,nickname,url,level,id])
    if(result.affectedRows){
        res.send({code:200,msg:"修改成功!"})
    }else{
        res.send({code:404,msg:"修改失败!"})
    }
}

/**
 * @Description:  # TODO 修改管理员密码
 * @Author: 文渊
 * @Date: 2023-09-05 14:30:39
 */
async function updatepassword(req,res){
    const {id,password} = req.body;
    const sql = "update admin set password = ? where aid = ?"
    const result = await connect(sql,[password,id])
    if(result.affectedRows){
        res.send({code:200,msg:"修改管理员密码成功"})
    }else{
        res.send({code:405,msg:"修改管理员密码失败"})
    }
}
module.exports = {
    checkcode,
    login,
    queryall,
    add,
    remove,
    queryone,
    edit,
    updatepassword
}